package lhc.alg.top100;

import java.util.HashSet;
import java.util.Set;

/**
 * description: https://leetcode.cn/problems/longest-substring-without-repeating-characters/
 *  无重复的最长子串 滑动窗口
 * author: hongchen.liao
 * date:  2022/9/15
 */
public class _3_ {

    class Solution {
        public int lengthOfLongestSubstring(String s) {
            int n = s.length();
            Set<Character> set = new HashSet<>();
            int ans = 0, rk = -1;
            for(int i = 0; i < n; i++){
                if(i > 0){
                    set.remove(s.charAt(i-1));
                }
                while(rk + 1 < n && !set.contains(s.charAt(rk+1))){
                    set.add(s.charAt(rk+1));
                    rk++;
                }
                ans = Math.max(ans, rk+1-i);
            }
            return ans;
        }
    }

}
